<template>
  <el-container>
    <el-main v-if="state.data">
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item label="核销数量" prop="write_num">
          <el-input type="number" v-model="form.model.write_num" placeholder="请输入数量"></el-input>
          <div class="sa-m-l-10">可核销{{ state.data.count }}张</div>
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button
        type="primary"
        v-auth="'seller.admin.grouporder.check'"
        @click="confirm"
      >确认
      </el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from './groupOrder.service';
  import { ElMessage } from 'element-plus';

  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });

  const state = reactive({
    data: null,
  });

  let formRef = ref(null);
  const form = reactive({
    model: {
      write_num: '',
    },
    rules: {
      write_num: [{ required: true, message: '请输入数量', trigger: 'blur' }],
    },
  });

  onMounted(() => {
    if (props.modal.params.id) {
      getData();
    }
  });

  const getData = async () => {
    const { error, data } = await api.checkDetails({
      id: props.modal.params.id,
    });

    if (error === 0) {
      state.data = data;
    }
  };

  const confirm = () => {
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      const { error } = await api.check({
        id: props.modal.params.id,
        write_num: form.model.write_num,
      });
      if (error === 0) {
        ElMessage({
          message: '操作成功',
          type: 'success',
        });
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  };
</script>

<style scoped lang="scss">
</style>
